
import 'package:cunyutech_flutter_ui/cunyutech_flutter_ui.dart';

class CYApplicationModel implements Model<CYApplicationStateTmpl> {
  @override
  Map<String, EffectFunction> effects = <String, EffectFunction>{
    "bootstrap": (Action action, ModelEffects modelEffects) async {
      var cyApplication = modelEffects.getCurrentState() as CYApplicationStateTmpl;
        if(cyApplication.enableProxy == true) {
        /// 测试
        CYRequest.httpProxy = cyApplication.httpProxy;
        CYRequest.httpsProxy = cyApplication.httpsProxy;
      }
    },
    "saveProxySettings":  (Action action, ModelEffects modelEffects) async {
      Map<String,dynamic> formData = action.payload as Map<String,dynamic>;
      var cyApplication = modelEffects.getCurrentState() as CYApplicationStateTmpl;
      cyApplication.enableProxy = formData["enableProxy"];
      cyApplication.httpProxy = formData["httpProxy"];
      cyApplication.httpsProxy = formData["httpsProxy"];

      CYRequest.httpProxy = cyApplication.enableProxy ? cyApplication.httpProxy : "";
      CYRequest.httpsProxy = cyApplication.enableProxy ? cyApplication.httpsProxy : "";
      showToast(msg: "保存成功");
    },
  };

  @override
  String namespace = "cyapplication";

  @override
  Map<String, ReducerFuction> reducers = <String, ReducerFuction>{
    "save": (StoreOfState<StateTmpl> lastState, Action action) {
      lastState.setState(action.payload);
      return lastState;
    }
  };

@override
  StoreOfState<CYApplicationStateTmpl> state = StoreOfState<CYApplicationStateTmpl>(state: CYApplicationStateTmpl(), updateAt: DateTime.now().toUtc());

}